/**
 * 
 */
package com.jushu.storbox.actions;

import java.util.Set;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import com.jushu.storbox.LoginActivity;
import com.jushu.storbox.ShareDriveApplication;
import com.jushu.storbox.constant.ClientConfig;
import com.jushu.storbox.manager.DownloadTaskManager;
import com.jushu.storbox.manager.UploadTaskManager;
import com.jushu.storbox.task.tqueue.DownloadTask;
import com.jushu.storbox.task.tqueue.UploadTask;
import com.jushu.storbox.util.PublicTools;
import com.huawei.sharedrive.sdk.android.common.Constants;

/**
 * Logout deal
 * 
 * 
 */
public class LogoutAction extends Actions {

	public LogoutAction(Context context) {
		super(context);
	}

	public void logout(Activity activity) {
		//cancel download task
		Set<DownloadTask> downloadTasks = DownloadTaskManager
				.getAllTaskes();
		if (downloadTasks != null && downloadTasks.size() > 0) {
			for (DownloadTask task : downloadTasks) {
				task.stopDownloadTask();
			}
		}
		//cancel upload task
		Set<UploadTask> uploadTasks = UploadTaskManager.getAllTaskes();
		if (uploadTasks != null && uploadTasks.size() > 0) {
			for (UploadTask task : uploadTasks) {
				task.stopUploadTask();
			}
		}
		SharedPreferences settings = activity.getSharedPreferences(
				ClientConfig.settings, Context.MODE_APPEND);
		Editor editor = settings.edit();
		editor.putBoolean(ClientConfig.AUTOLOGIN, false);
		PublicTools.setExpiredAt(activity, 0l);
		editor.putString(ClientConfig.USERNAME, "");
		PublicTools.setAuthorization(activity, "");
		PublicTools.setOwnerId(activity, "");
		editor.putString(ClientConfig.LOGINPASSWORD, "");
		editor.putBoolean(ClientConfig.NOT_NEED_NOTIFY, false);//
		PublicTools.setClientUserPassFail(activity, "");
		ShareDriveApplication.getInstance().setAuthorization(null);
		ShareDriveApplication.getInstance().setWnerID(null);
		editor.putString(ClientConfig.UAM_ADDRESS, "");
		editor.putString(ClientConfig.UFM_ADDRESS, "");
		editor.commit();
		Constants.UAM_ADDRESS = "";
		Constants.UFM_ADDRESS = "";

		Intent intent = new Intent();
		intent.setClass(activity, LoginActivity.class);
		try {
			// ActivityTaskManager.getInstance().closeAllActivity();
			activity.finish();
		} finally {
			activity.startActivity(intent);
		}
	}
}
